package com.waya.web.browser;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.waya.web.browser.manager.WbWebManager;
import com.waya.web.browser.player.view.MediaPlayerLayout;
import com.waya.web.browser.webview.WebDefaultViewClient;

import java.lang.ref.WeakReference;


/**
 * Created by JesseHev on 2018/1/18.
 * describe:
 */

public class WayeWebViewDialog extends AbsDialog {

    private WebView mWebView;
    private MediaPlayerLayout mPlayLayout;
    private WbWebManager mWebManager;
    private WeakReference<Context> mContext;
    private String mUrl;
    private String mVideoUrl = "http://111.161.5.36/h5/Public/Home/video/video_20160212.mp4";

    private int mWidth;
    private int mHeight;
    private int mBg;


    public WayeWebViewDialog(Context context, String url, int width, int height) {
        this(context, url, width, height, -1);
    }

    public WayeWebViewDialog(Context context, String url, int width, int height, @IdRes int bg) {
        super(context, R.style.web_dialog);
        mContext = new WeakReference<>(context);
        mUrl = url;
        mWidth = width;
        mHeight = height;
        mBg = bg;
        mWebView = (WebView) findViewById(R.id.webView);
        mPlayLayout = (MediaPlayerLayout) findViewById(R.id.play_layout);
        creatWindowParam();
        init();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mWebManager != null) {
            mWebManager.destroy();
        }
    }

    private void init() {

        mUrl = "http://h5games.qcast.cn/happycastle_unicom/gamehall/index.html";

        mWebManager = WbWebManager.getInstance(mContext.get());
        mWebManager.setTarget(mWebView)
                .setCookie(mUrl, "JSEESIONID=0000000000000")

                .setSetting()
                .dialog(this)
                .palyerLayout(mPlayLayout)
                .registerJsObj()
                .setChromClient(getWebChromeClient())
                .setWebClient(getWebViewClient())
                .load(mUrl);
    }

    public void creatWindowParam() {
        if (getWindow() != null) {

            WindowManager.LayoutParams params = getWindow().getAttributes();
            if (mWidth == -1 || mHeight == -1) {
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.height = WindowManager.LayoutParams.MATCH_PARENT;
            } else {
                params.width = mWidth;
                params.height = mHeight;
            }
            if (mBg != -1) {
                getWindow().setBackgroundDrawable(
                        getContext().getResources().getDrawable(R.drawable.played_chanel_bg));
            }
        }
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_web_view;
    }

    private WebChromeClient getWebChromeClient() {
        return new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                System.out.println("progress===>" + newProgress);
                mWebView.setVisibility(View.VISIBLE);
                mWebView.requestFocus();
                super.onProgressChanged(view, newProgress);
            }
        };
    }

    private WebViewClient getWebViewClient() {
        return new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mWebView.requestFocus();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description,
                                        String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        };
    }

}
